package org.example.test;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONSupport;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;


public class StreamTypeBug {

    public static void main(String[] args) {
        List<ToolAuctionOrderListDTO> merchantInfoList = new ArrayList<>();
        List<ToolAuctionOrderListDTO.ToolAuctionOrderListItem> cooperatorInfoList = new ArrayList<>();
        ToolAuctionOrderListDTO.ToolAuctionOrderListItem  cooperatorInfo =ToolAuctionOrderListDTO.ToolAuctionOrderListItem.builder().name("1").avatar("1").cooperatorCoordinate(new ArrayList<>()).build();
        ToolAuctionOrderListDTO.ToolAuctionOrderListItem  cooperatorInfo01 =ToolAuctionOrderListDTO.ToolAuctionOrderListItem.builder().name("01").avatar("1").cooperatorCoordinate(new ArrayList<>()).build();
        ToolAuctionOrderListDTO.ToolAuctionOrderListItem  cooperatorInfo02 =ToolAuctionOrderListDTO.ToolAuctionOrderListItem.builder().name("02").avatar("1").cooperatorCoordinate(new ArrayList<>()).build();
        ToolAuctionOrderListDTO.ToolAuctionOrderListItem  cooperatorInfo03 =ToolAuctionOrderListDTO.ToolAuctionOrderListItem.builder().name("03").avatar("1").cooperatorCoordinate(new ArrayList<>()).build();

        cooperatorInfoList.add(cooperatorInfo);
        cooperatorInfoList.add(cooperatorInfo01);
        cooperatorInfoList.add(cooperatorInfo02);
        cooperatorInfoList.add(cooperatorInfo03);

        ToolAuctionOrderListDTO  toolAuctionOrderListDTO = ToolAuctionOrderListDTO.builder().cooperatorInfo(cooperatorInfoList).build();
        merchantInfoList.add(toolAuctionOrderListDTO);
        List<ToolAuctionOrderListResult.ToolAuctionOrderListItem> res = buildItemsV2(merchantInfoList, 2121212121L, 100L, new ArrayList<>());
        System.out.println(JSON.toJSON(res));
    }

    private static List<ToolAuctionOrderListResult.ToolAuctionOrderListItem> buildItemsV2(
        List<ToolAuctionOrderListDTO> merchantInfoList, Long finishTime, Long price, List<Double> coordinate) {
        Random random = new Random();
        if (!CollectionUtil.isEmpty(merchantInfoList)) {
            int size = random.nextInt(merchantInfoList.size());
            ToolAuctionOrderListDTO toolAuctionOrderListDTO = merchantInfoList.get(size);
            if (!CollectionUtil.isEmpty(toolAuctionOrderListDTO.getCoordinate())) {
                coordinate.addAll(toolAuctionOrderListDTO.getCoordinate());
            }
            List<ToolAuctionOrderListDTO.ToolAuctionOrderListItem> cooperatorInfoList =
                toolAuctionOrderListDTO.getCooperatorInfo();
            long minPrice = Math.round(price * 0.8);
            long rangePrice = (price - minPrice) / cooperatorInfoList.size();
            List<ToolAuctionOrderListResult.ToolAuctionOrderListItem> resultList = new ArrayList<>();
            for (int i = 0; i < cooperatorInfoList.size(); i++) {
                ToolAuctionOrderListDTO.ToolAuctionOrderListItem cooperatorInfo = cooperatorInfoList.get(i);
                long randomFactor = i * rangePrice + random.nextInt(Math.toIntExact(rangePrice));
                resultList.add(
                    ToolAuctionOrderListResult.ToolAuctionOrderListItem.builder().name(cooperatorInfo.getName())
                        .avatar(cooperatorInfo.getAvatar()).province(cooperatorInfo.getProvince())
                        .cooperatorCoordinate(cooperatorInfo.getCooperatorCoordinate())
                        .offerPrice(minPrice + randomFactor).build());
            }
            return resultList;
        }
        return Collections.emptyList();
    }
}
